﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;

using Sumen.Library.Classes.Common;
using Sumen.Library.Enum;
using Sumen.Model.Model;

namespace Sumen.Business.Presenter.Sys
{
    public class SysLogPresenter
    {

        public int Add(EnumFormCode pFromCode, EnumFormStatus pAction, string pRefId, string pDesc)
        {
            using (sumenEntitiesSystem objEntitiesSystem = new sumenEntitiesSystem())
            {
                try
                {
                    sys_log log = new sys_log();
                    log.Sys_MChine = System.Environment.MachineName;
                    log.Sys_AccountWin = Environment.UserName;
                    log.Sys_UserID = ClsPublic.User.User_Username;
                    log.Sys_FullName = ClsPublic.User.User_FullName;
                    log.Sys_Created = DateTime.Now;
                    log.Sys_Object_ID = pFromCode.ToString();
                    log.Sys_Action = (int)pAction;
                    log.Sys_Action_Name = GetActionName(pAction);
                    log.Sys_Reference = pRefId;
                    log.Sys_IPLan = LocalIPAddress();
                    //log.Sys_IPWan = ClsPublic.IpWan + string.Empty;
                    log.Sys_Mac = "";
                    log.Sys_DeviceName = "";
                    log.Sys_Description = pDesc;
                    log.Sys_Actived = 1;
                    objEntitiesSystem.sys_log.Add(log);
                    return objEntitiesSystem.SaveChanges();
                }
                catch (Exception ex)
                {
                    ClsPublic.WriteException(ex);
                    ClsMsgBox.Loi("Có lỗi trong quá trình ghi nhật ký hệ thống.");
                    return 0;
                }
            }
        }

        public ICollection<object> GetLog(DateTime pBegin, DateTime pEnd)
        {
            using (sumenEntitiesSystem objEntitiesSystem = new sumenEntitiesSystem())
            {
                objEntitiesSystem.Configuration.LazyLoadingEnabled = false;
                objEntitiesSystem.Configuration.ProxyCreationEnabled = false;
                var query = from p in objEntitiesSystem.sys_log
                            where p.Sys_Created >= pBegin && p.Sys_Created <= pEnd
                            join p1 in objEntitiesSystem.sys_object on p.Sys_Object_ID equals p1.Syso_ID into Inners
                            from inner in Inners.DefaultIfEmpty()
                            select new
                            {
                                p.Sys_AccountWin,
                                p.Sys_UserID,
                                p.Sys_FullName,
                                p.Sys_IPLan,
                                p.Sys_IPWan,
                                p.Sys_Action_Name,
                                inner.Syso_Name,
                                p.Sys_Created,
                                p.Sys_MChine,
                                p.Sys_Reference
                            };
                return new[] { query.ToList() };
            }
        }

        string GetActionName(EnumFormStatus pAction)
        {
            string action = "";
            switch (pAction.ToString())
            {
                case "View":
                    action = "Xem";
                    break;
                case "Add":
                    action = "Thêm";
                    break;
                case "Modify":
                    action = "Sửa";
                    break;
                case "Delete":
                    action = "Xóa";
                    break;
                case "Save":
                    action = "Lưu";
                    break;
                case "Cancel":
                    action = "Hủy bỏ";
                    break;
            }
            return action;
        }

        string LocalIPAddress()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                    break;
                }
            }
            return localIP;
        }




    }
}
